#include"SettingsDialog.h"

#include<QDesktopServices>
#include<QDialogButtonBox>
#include<QFileDialog>
#include<QFormLayout>
#include<QLabel>
#include<QMessageBox>
#include<QPushButton>
#include<QTabWidget>
#include<QUrl>
#include<QVBoxLayout>
#include<QWidget>

#include"Settings.h"

#include<stdio.h>

SettingsDialog::SettingsDialog():QDialog(0){
// 	setAttribute(Qt::WA_DeleteOnClose);
	QVBoxLayout *layout = new QVBoxLayout(this);
	setLayout(layout);
	
	QTabWidget *tabWidget = new QTabWidget(this);
	tabWidget->setTabPosition(QTabWidget::West);
	layout->addWidget(tabWidget);
	
	QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Apply|QDialogButtonBox::Cancel, Qt::Horizontal, this);
	layout->addWidget(buttons);
	connect(buttons, SIGNAL(clicked(QAbstractButton*)), this, SLOT(slotButtonClicked(QAbstractButton*)));
	
	//General
	QWidget *generalPage = new QWidget(tabWidget);
	tabWidget->addTab(generalPage, "General");
	QVBoxLayout *generalLayout = new QVBoxLayout(generalPage);
	generalPage->setLayout(generalLayout);
	
	QWidget *widget = new QWidget(generalPage);
	QHBoxLayout *hboxLayout = new QHBoxLayout(widget);
	widget->setLayout(hboxLayout);
	hboxLayout->addWidget(new QLabel( "Music Directory:", widget));
	m_editGeneralMusicDir = new QLineEdit(widget);
	m_editGeneralMusicDir->setReadOnly(true);
	m_editGeneralMusicDir->setText(Settings::instance()->general->musicDir);
	hboxLayout->addWidget(m_editGeneralMusicDir);
	QPushButton *button = new QPushButton(QIcon::fromTheme("folder-open"), "", widget);
	hboxLayout->addWidget(button);
	connect(button, SIGNAL(clicked()), this, SLOT(slotSetGeneralMusicDir()));
	generalLayout->addWidget(widget);
	
	m_checkboxGeneralUsePlaylist = new QCheckBox("Save/Load playlist on exit/start", generalPage);
	m_checkboxGeneralUsePlaylist->setChecked(Settings::instance()->general->usePlaylist);
	connect(m_checkboxGeneralUsePlaylist, SIGNAL(stateChanged(int)), this, SLOT(slotGeneralChanged()));
	generalLayout->addWidget(m_checkboxGeneralUsePlaylist);
	
	//LastFM
	QWidget *lastFMPage = new QWidget(tabWidget);
	tabWidget->addTab(lastFMPage, "Last.FM");
	QVBoxLayout *lastFMLayout = new QVBoxLayout(lastFMPage);
	lastFMPage->setLayout(lastFMLayout);
	
	QLabel *labelWidget = new QLabel(lastFMPage);
	labelWidget->setPixmap(QPixmap("../Lastfm_LogosAndGuidelines/Logos/Last.fm_Logo_Black.png").scaledToHeight(50));
	lastFMLayout->addWidget(labelWidget);
	
	button = new QPushButton("Sign up!", lastFMPage);
	lastFMLayout->addWidget(button);
	connect(button, SIGNAL(clicked()), this, SLOT(slotLastFMSignup()));
	
	m_checkboxLastFMScrobble = new QCheckBox("Scrobble tracks to Last.FM", lastFMPage);
	m_checkboxLastFMScrobble->setChecked(Settings::instance()->lastfm->isScrobbling);
	connect(m_checkboxLastFMScrobble, SIGNAL(stateChanged(int)), this, SLOT(slotLastFMChanged()));
	lastFMLayout->addWidget(m_checkboxLastFMScrobble);
	
	m_checkboxLastFMCorrections = new QCheckBox("Offer to make corrections to meta-data", lastFMPage);
	m_checkboxLastFMCorrections->setChecked(Settings::instance()->lastfm->offerCorrections);
	connect(m_checkboxLastFMCorrections, SIGNAL(stateChanged(int)), this, SLOT(slotLastFMChanged()));
	lastFMLayout->addWidget(m_checkboxLastFMCorrections);
}

void SettingsDialog::slotButtonClicked(QAbstractButton *button){
	if(button->text() == "OK")
		buttonOk();
	else if(button->text() == "Cancel")
		buttonCancel();
	else if(button->text() == "Apply")
		buttonApply();
}

void SettingsDialog::buttonCancel(){
	Settings::instance()->general->_changed = false;
	Settings::instance()->lastfm->_changed = false;
	done(QDialog::Rejected);
}

void SettingsDialog::buttonOk(){
	if( buttonApply() )
		done(QDialog::Accepted);
}

bool SettingsDialog::buttonApply(){
	if(!Settings::instance()->isChanged())
		return true;

	if( QMessageBox::question(this, "Confirmation", "Are you sure you wish to write this to file?", QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel) == QMessageBox::Cancel )
		return false;
	//General page
	Settings::instance()->general->musicDir = m_editGeneralMusicDir->text();
	Settings::instance()->general->usePlaylist = m_checkboxGeneralUsePlaylist->isChecked();
	
	//LastFM page
	Settings::instance()->lastfm->isScrobbling = m_checkboxLastFMScrobble->isChecked();
	Settings::instance()->lastfm->offerCorrections = m_checkboxLastFMCorrections->isChecked();
	
	Settings::instance()->write();
	return true;
}

void SettingsDialog::slotLastFMSignup(){
	QDesktopServices::openUrl( QUrl("https://www.last.fm/join") );
}

void SettingsDialog::slotSetGeneralMusicDir(){
	QString dir = QFileDialog::getExistingDirectory(this, "Select Music Folder", Settings::instance()->general->musicDir);
	if(!dir.isEmpty()){
		m_editGeneralMusicDir->setText(dir);
		Settings::instance()->general->_changed = true;
	}
}

void SettingsDialog::slotLastFMChanged(){
	Settings::instance()->lastfm->_changed = true;
}

void SettingsDialog::slotGeneralChanged(){
	Settings::instance()->general->_changed = true;
}

void SettingsDialog::show(){
	SettingsDialog *dialog = new SettingsDialog();
	dialog->exec();
	printf("Deleting dialog...");
	delete dialog;
}
